/**
 * File with basic layouts
 */

/**
 * Base layout class
 * @type {omplusClass}
 */
Omplus.layouts.Layout = Omplus.base.extend(Omplus.base.Base, {

    /**
     * Starts organize
     */
    init:function () {
        this.organize();
    },

    omplusType:"Layout",

    /**
     * jQuery object of layout and its children
     * @type {jQuery}
     */
    content:null,

    layout:null,

    aligment:null,

    height:"auto",

    width:"100%",

    /**
     * Contains all the options availiable in this layout
     */
    options:{},

    /**
     * Sets layout
     */
    organize:function () {
        this.layout = jQuery.extend(true, {}, $("<div></div>"));
        this.layout.addClass("layout");
        if (this.width) {
            this.layout.css({width:this.width});
        }
        if (this.height) {
            this.layout.css({height:this.height});
        }
        this.content.append(this.layout);
        this.organizeLayout();
    },

    organizeLayout:function () {
    },

    /**
     * Appends content of its children to this layouts content
     * @param {omplusClass}
        */
    append:function (control) {
        Omplus.globals.getObject(control).getContent().appendTo(this.layout);
    }
});

Omplus.layouts.LinearLayout = Omplus.base.extend(Omplus.layouts.Layout, {
    omplusType:"LinearLayout",
    orientation:null,
    organizeLayout:function () {
        this.layout.addClass(this.aligment);
        this.layout.addClass(this.orientation);
    }
});

Omplus.layouts.HorizontalLayout = Omplus.base.extend(Omplus.layouts.LinearLayout, {
    omplusType:"HorizontalLayout",
    orientation:"horizontal"
});

Omplus.layouts.VerticalLayout = Omplus.base.extend(Omplus.layouts.LinearLayout, {
    omplusType:"VerticalLayout",
    orientation:"vertical"
});

Omplus.layouts.PaginationLayout = Omplus.base.extend(Omplus.layouts.Layout, {
    omplusType:"PaginationLayout",
    orientation:null,
    itemsPerPage:null,
    height: "100%",
    organizeLayout:function () {
        this.layout.addClass("pagination-" + this.orientation);
        var pages = jQuery.extend(true, {}, $("<div></div>"));
        pages.addClass("pages");
        this.layout.append(pages).addClass("pagination-" + this.orientation);
    },
    appendPagination:function (properties) {
        var pagination = Omplus.base.create(Omplus.views.Pagination, properties);
        this.layout.parent().append(pagination.getContent());
    }
});

Omplus.layouts.HorizontalPaginationLayout = Omplus.base.extend(Omplus.layouts.PaginationLayout, {
    omplusType:"HorizontalPaginationLayout",
    orientation:"horizontal",
    pageWidth:null,
    rowsPerPage:null,
    rowHeight:null,
    append:function (control) {
        if (this.pageWidth == null) {
            this.pageWidth = 100;
            var height = parseInt(this.rowHeight.substring(0, this.rowHeight.length - 2));
            this.layout.css({"height":(height * this.rowsPerPage) + "px", "width":this.pageWidth + "%"}).find(".pages").css({"width":this.pageWidth + "%"});
            // pagination height = 55px
            this.layout.parent().parent().css({height:(height * this.rowsPerPage + 55) + "px"})
        }
        if (!this.layout.find(".page").length || this.layout.find(".page:last").find(".pageRow").length == this.rowsPerPage) {
            var page = jQuery.extend(true, {}, $("<div></div>"));
            var height = parseInt(this.rowHeight.substring(0, this.rowHeight.length - 2));
            page.addClass("page").css({"height":(height * this.rowsPerPage) + "px", "width":this.pageWidth + "%"});
            this.layout.find(".pages").append(page).css({"width":(this.pageWidth * this.layout.find(".page").length) + "%"}).find(".page").css(
                {"width":100 / this.layout.find(".pages").find(".page").length + "%"}
            );
        }
        Omplus.globals.getObject(control).getContent().addClass("pageRow").css({"height":this.rowHeight}).appendTo(this.layout.find(".page:last"));
    }
});

Omplus.layouts.VerticalPaginationLayout = Omplus.base.extend(Omplus.layouts.PaginationLayout, {
    omplusType:"VerticalPaginationLayout",
    orientation:"vertical",
    rowsPerPage:null,
    rowHeight:null,
    append:function (control) {
        var height = parseInt(this.rowHeight.substring(0, this.rowHeight.length - 2));
        this.layout.css({"height":(height * this.rowsPerPage) + "px"});
        if (this.pageWidth == null) {
            this.pageWidth = this.layout.width();
        }
        if (!this.layout.find(".page").length || this.layout.find(".page:last").find(".pageRow").length == this.rowsPerPage) {
            var page = jQuery.extend(true, {}, $("<div></div>"));
            var height = parseInt(this.rowHeight.substring(0, this.rowHeight.length - 2));
            page.addClass("page").css({"height":(height * this.rowsPerPage) + "px"});
            this.layout.find(".pages").append(page);
        }
        Omplus.globals.getObject(control).getContent().addClass("pageRow").css({"height":this.rowHeight}).appendTo(this.layout.find(".page:last"));
    }
});

Omplus.layouts.AccordionLayout = Omplus.base.extend(Omplus.layouts.Layout, {
    init:function () {
    },

    omplusType:"AccordionLayout",

    organizeLayout:function () {
        this.layout.attr("id", this.omplusName.replace(".", "_"));

    },
    organize:function () {
        this.layout = jQuery.extend(true, {}, $("<div></div>"));
        this.layout.css({height:"100%", width:"100%"});
        this.layout.addClass("accordionLayout");
        this.content.append(this.layout);
        this.organizeLayout();
        this.layout.accordion();
    },
    append:function (control) {
        this.layout.append(Omplus.globals.getObject(control).getTitleContent());
        this.layout.append(Omplus.globals.getObject(control).getContent());
        this.layout.accordion("destroy");
        this.layout.accordion();
        this.layout.find(".ui-accordion-content").css({"overflow":"hidden", height:"100%"});
    }
});

Omplus.layouts.TabLayout = Omplus.base.extend(Omplus.layouts.Layout, {
    init:function () {
    },

    omplusType:"TabLayout",

    titles:null,

    selected:-1,

    organizeLayout:function () {
        this.layout.attr("id", this.omplusName.replace(".", "_"));
    },
    organize:function () {
        this.layout = jQuery.extend(true, {}, $("<div></div>"));
        this.layout.height("100%");
        this.layout.width("100%");
        this.layout.addClass("tabLayout");

        this.titles = jQuery.extend(true, {}, $("<ul></ul>"));
        this.layout.append(this.titles);
        this.content.append(this.layout);
        this.organizeLayout();
        this.layout.tabs({
            hide: {
                effect: "fade",
                duration: 300
            },
            show: {
                effect: "fade",
                duration: 300
            }
        });
    },
    append:function (control) {
        this.titles.append(Omplus.globals.getObject(control).getTitleContent());
        this.layout.append(Omplus.globals.getObject(control).getContent());
        this.layout.tabs("refresh");
        this.layout.tabs( "option", "active", 0);
    },

    activate: function(tab){
        this.layout.tabs( "option", "active", tab);
    },
    remove : function (index){
;        this.layout.tabs('remove', index);
        this.layout.tabs("refresh");
        this.layout.tabs( "option", "active", 0);
    }
});


Omplus.layouts.OpeningTabLayout = Omplus.base.extend(Omplus.layouts.Layout, {
    init:function () {
    },

    omplusType:"OpeningTabLayout",

    selected:-1,

    organizeLayout:function () {
        this.layout.attr("id", this.omplusName.replace(".", "_"));
    },
    organize:function () {
    },
    append:function (control) {
    }
});

Omplus.layouts.DinamicLayout = Omplus.base.extend(Omplus.layouts.Layout, {
    omplusType:"DinamicLayout",
    height: "100%",
    rowHeight:null,
    organizeLayout:function () {
        this.layout.addClass("dinamic");
        var pages = jQuery.extend(true, {}, $("<div></div>"));
        pages.addClass("pages");
        this.layout.append(pages).addClass("dinamic");
    },
    appendPagination:function (properties) {
        var pagination = Omplus.base.create(Omplus.views.Pagination, properties);
        this.layout.parent().append(pagination.getContent());
    },
    append:function (control) {
        Omplus.globals.getObject(control).getContent().addClass("pageRow").css({"height":this.rowHeight}).appendTo(this.layout.find(".pages"));
    }
});